﻿<%@ WebHandler Language="C#" Class="TBSChannel" %>

using System;
using System.Web;
using System.Data;
using DBUtility;

/// <summary>
/// 信道处理程序
/// </summary>
public class TBSChannel : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string strReturn = "{\"rows\":[]}";
        /*获取上行占用率*/
        if (context.Request.Params["uplink"] != null)
        {
            DataSet ds = new BLL.Topo.CHANNEL().GetMainChanelUplinkList(context.Request.Params["top"], context.Request.Params["strWhere"] == null ? "" : context.Request.Params["strWhere"]);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                strReturn = DataTableToJSON(ds.Tables[0]);
            }
        }
        /*获取下行占用率*/
        if (context.Request.Params["downlink"] != null)
        {
            DataSet ds = new BLL.Topo.CHANNEL().GetMainChanelDownlinkList(context.Request.Params["top"], context.Request.Params["strWhere"] == null ? "" : context.Request.Params["strWhere"]);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                strReturn = DataTableToJSON(ds.Tables[0]);
            }
        }
        /*获取基站占用率*/
        if (context.Request.Params["link"] != null)
        {
            DataSet ds = new BLL.Topo.CHANNEL().GetMainChanelLinkList(context.Request.Params["top"], context.Request.Params["neid"]);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                strReturn = DataTableToJSON(ds.Tables[0]);
            }
        }
        /*获取业务信道占用率*/
        if (context.Request.Params["ywlink"] != null)
        {
            DataSet ds = new BLL.Topo.CHANNEL().GetChannelLink(context.Request.Params["top"]);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                strReturn = DataTableToJSON(ds.Tables[0]);
            }
        }
        /*获取基站性能指标详细*/
        if (context.Request.Params["getperformance"] != null)
        {
            strReturn = GetPerformance(context.Request.Params["tbsidlist"], context.Request.Params["starttime"],
             context.Request.Params["endtime"]);
        }
        /*获取方案列表*/
        /*if (context.Request.Params["getprogram"] != null)
        {
            DataSet ds = new BLL.Topo.InformationUtilization().GetProgramList("", context.Request.Params["programtype"]);
            if (ds != null  && ds.Tables.Count > 0) {
                     strReturn = DataTableToJSON(ds.Tables[0]);
            }
           
        }*/
        /*获取方案基站列表*/
       /* if (context.Request.Params["getprogramtbs"] != null)
        {
            DataSet ds = new BLL.Topo.InformationUtilization().GetProgramTBSList(context.Request.Params["programid"]);
            string TBSList = string.Empty;
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0, length = ds.Tables[0].Rows.Count; i < length; i++)
                {
                    TBSList += ds.Tables[0].Rows[i]["TBSNEID"].ToString() + ",";
                }
            }
            strReturn = "{\"result\":\"" + TBSList + "\"}";
        }*/
        /*获取视频IP地址列表*/
        if (context.Request.Params["updatetbs"] != null)
        {
            int n = new BLL.Topo.InformationUtilization().ExcelSQL(context.Request.Params["sql"].ToString());
            strReturn = "{\"result\":\"" + (n > 0 ? true : false) + "\"}";
        }
        context.Response.ContentType = "json";
        context.Response.Write(strReturn);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    /// <summary>
    /// 将DATATABLE转化成JSON字符串
    /// </summary>
    /// <param name="dt">数据集</param>
    /// <returns></returns>
    private string DataTableToJSON(DataTable dt)
    {
        string strReturn = string.Empty;
        if (dt != null && dt.Rows.Count > 0)
        {
            strReturn = "{\"rows\":[";
            int length = dt.Columns.Count;
            int colCount = length;
            string[] columns = new string[length];
            object orv = new object();
            for (int i = 0; i < length; i++)
            {
                columns[i] = dt.Columns[i].ColumnName;
            }
            length = dt.Rows.Count;

            for (int i = 0; i < length; i++)
            {
                strReturn += "{";
                for (int j = 0; j < colCount; j++)
                {
                    orv = dt.Rows[i][columns[j]];
                    strReturn += "\"" + columns[j].ToUpper().ToString() + "\":\"" + (orv == null ? "" : orv.ToString()) + "\"";
                    if (j < colCount - 1)
                    {
                        strReturn += ",";
                    }
                }
                strReturn += "}";
                if (i < length - 1)
                {
                    strReturn += ",";
                }
            }
            strReturn += "]}";

        }
        return strReturn;
    }
    /// <summary>
    /// 获取基站性能数据
    /// </summary>
    /// <param name="TBSID">基站ID列表</param>
    /// <param name="STARTTIME1">第一个查询时间</param>
    /// <param name="STARTTIME2">第二个查询时间</param>
    /// <returns></returns>
    public string GetPerformance(string TBSID, string STARTTIME1, string STARTTIME2)
    {
        try
        {
            string tbsidStr = TBSID;

            string[] tbsID = tbsidStr.Split(',');
            string st1 = STARTTIME1 + " 00:00:00";
            string et1 = STARTTIME1 + " 23:59:59";
            string strReturn = "{\"rows\":[";
            string[] typeList = { "UPLINK", "DOWNLINK", "CHAN_COUNT", "MESSAGE_COUNT", "QIEHUAN_COUNT", "UPDATES_COUNT" };
            strReturn += "{\"STATICTIME\":\"今天\",\"rows\":[";
            DataSetHelper dsh = new DataSetHelper();
            DataTable dt = new DataTable();
            for (int j = 0, count = typeList.Length; j < count; j++)
            {
                DataSet ds = GetTBSPerformance("", st1, et1, "'" + TBSID.Replace(",", "','") + "'", typeList[j]);

                // string strData = string.Empty;
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    strReturn += "{\"STATICTYPE\":\"" + typeList[j] + "\",\"rows\":[";

                    for (int i = 0, length = tbsID.Length; i < length; i++)
                    {
                        dt = dsh.RowToTable(ds.Tables[0].Select("NE_ID='" + tbsID[i] + "'"));
                        if (dt != null)
                        {
                            strReturn += DataTableToJSON(dt).Insert(1, "\"NE_ID\":\"" + tbsID[i] + "\",");

                        }
                        else
                        {
                            strReturn += "{\"rows\":[]}";
                        }
                        if (i < length - 1)
                        {
                            strReturn += ",";
                        }
                    }
                    strReturn += "]}";
                }
                else
                {
                    strReturn += "{\"STATICTYPE\":\"" + typeList[j] + "\",\"rows\":[]}";
                }
                if (j < count - 1)
                {
                    strReturn += ",";
                }
            }
            st1 = STARTTIME2 + " 00:00:00";
            et1 = STARTTIME2 + " 23:59:59";
            strReturn += "]},{\"STATICTIME\":\"历史\",\"rows\":[";
            for (int j = 0, count = typeList.Length; j < count; j++)
            {
                DataSet ds = GetTBSPerformance("", st1, et1, "'" + TBSID.Replace(",", "','") + "'", typeList[j]);

                // string strData = string.Empty;
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    strReturn += "{\"STATICTYPE\":\"" + typeList[j] + "\",\"rows\":[";
                    for (int i = 0, length = tbsID.Length; i < length; i++)
                    {
                        dt = dsh.RowToTable(ds.Tables[0].Select("NE_ID='" + tbsID[i] + "'"));
                        if (dt != null)
                        {
                            strReturn += DataTableToJSON(dt).Insert(1, "\"NE_ID\":\"" + tbsID[i] + "\",");

                        }
                        else
                        {
                            strReturn += "{\"rows\":[]}";
                        }
                        if (i < length - 1)
                        {
                            strReturn += ",";
                        }
                    }
                    strReturn += "]}";

                }
                else
                {
                    strReturn += "{\"STATICTYPE\":\"" + typeList[j] + "\",\"rows\":[]}";
                }
                if (j < count - 1)
                {
                    strReturn += ",";
                }
            }
            return strReturn + "]}]}";
        }
        catch (Exception ex)
        {
            //throw;
        }
        return null;
    }
    /// <summary>
    /// 调用存储过程获取基站详细监控指标
    /// </summary>
    /// <param name="NEID">基站ID</param>
    /// <param name="STIME">开始时间</param>
    /// <param name="ETIME">结束时间</param>
    /// <param name="STRWHERE">查询条件</param>
    /// <returns></returns>
    private DataSet GetTBSPerformance(string NEID, string STIME, string ETIME, string STRWHERE, string TYPE)
    {
        string[,] str = new string[5, 2] { { "p_ne_id", "string" }, { "p_stime", "datetime" }, { "p_etime", "datetime" }, { "p_strwhere", "string" }, { "p_type", "string" } };
        string[] v = new string[] { NEID, STIME, ETIME, STRWHERE, TYPE };
        return new BLL.Common.mysqldata().GetList("GET_TBS_DETAIL_INDITACOR1", str, v);
    }
}